import { Button, Form, Input, message, } from 'antd';
import { useState } from 'react'
import { useNavigate } from 'react-router-dom';
import '../assets/css/login.css'
import { userLogin } from '../request/api'
function Login() {
    const Navigate = useNavigate();
    const onFinish = (values) => {
        console.log("Success:", values);
        userLogin(values).then((res) => {
            console.log(res, '登录结果');
            if (res.code == 200) {
                message.success(res.msg)
                // 存储在会话存储中
                sessionStorage.setItem('userinfo', JSON.stringify(res.list))
                // 跳转到index页面
                Navigate("/index");
            } else {
                message.error(res.msg)
            }
        })
        //调用登录接口 类似于vue中push 

    }
    // 封装一个确定事件
   
    const onFinishFailed = (errorInfo) => {
        console.log('Failed:', errorInfo);
    };
    return (
        <div className='box'>
            <Form
                name="basic"
                labelCol={{
                    span: 8
                }}
                wrapperCol={{
                    span: 16
                }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
            >
                <Form.Item
                    label="用户名"
                    name="username"
                    rules={[
                        {
                            required: true,
                            message: '请输入你的用户名'
                        },
                    ]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="密码"
                    name="password"
                    rules={[
                        {
                            required: true,
                            message: '请输入你的密码'
                        },
                    ]}
                >
                    <Input.Password />
                </Form.Item>

                <Form.Item wrapperCol={{
                    offset: 8, span: 16
                }}>
                    <Button type="primary" htmlType="submit" >
                        登录
                    </Button>
                </Form.Item>
            </Form>
        </div>
    );
}

export default Login